#include "Log.h"

Log::Log() {
    log = new TStringList;
}

Log::Log(TStringList *Source) {
    log = new TStringList;
    log->Assign(Source);
}

Log::~Log() {
    delete log;
}

void Log::Assign(TStringList *Source) {
    this->Clear();
    this->log->Assign(Source);
    return;
}

void Log::Clear() {
    log->Clear();
}

Log* Log::clean(Plugin *plugin, Config *config) {
    Log *result = new Log();
    
    for (int i = 0; i < log->Count; ++i) {
        AnsiString line = log->Strings[i];
        const char *msg  = line.c_str();
        MessageType mt = mt = plugin->getType(msg);
        unsigned int flag = config->getFlag();
        switch (mt) {
            case SAY:
                if (flag & DEL_SAY)
                    continue;
                break;
            case YELL:
                if (flag & DEL_YELL)
                    continue;
                break;

            case WHISPER:
                if (flag & DEL_WHISPER)
                    continue;
                break;

            case EMOTE:
                if (flag & DEL_EMOTE)
                    continue;
                break;

            case RAID:
                if (flag & DEL_RAID)
                    continue;
                break;

            case CHANNEL:
                if (flag & DEL_CHANNEL)
                    continue;
                break;

            case UNKNOWN:
                if (flag & DEL_UNKNOWN)
                    continue;
                break;                                                                            
            default:
                continue;
        }
        if (config->isDeleteTime() && plugin->hasTime(msg))
            line = plugin->removeTime(msg).c_str();
        result->log->Add(line);
    }

    return result;
}

void Log::SaveToFile(AnsiString FileName) {
    this->log->SaveToFile(FileName);
}

void Log::LoadFromFile(AnsiString FileName) {
    this->Clear();
    this->log->LoadFromFile(FileName);
}


void Log::appendFromFile(AnsiString fileName) {
    TStringList *temp = new TStringList();
    temp->LoadFromFile(fileName);
    //UTF8ToAnsiList(temp);
    this->log->AddStrings(temp);
    delete temp;
}

void Log::changeCP() {
    UTF8ToAnsiList(log);
}

TStringList* Log::GetAll() {
    return this->log;
}

// Converts Entire list.
TStringList* UTF8ToAnsiList(TStrings* Source) {
    for (int i=0; i<Source->Count; i++) {
        Source->Strings[i] = Utf8ToAnsi(Source->Strings[i]);
    }
    return NULL;
}
